package com.itheima.bigdecimal;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalTest {
    public static void main(String[] args) {
       /* 有以下double数组：
        double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
        请编程计算它们的总值及平均值(四舍五入保留小数点后2位)*/
        double[] arr = {0.1, 0.2, 2.1, 3.2, 5.56, 7.21};
        BigDecimal[] brr = new BigDecimal[arr.length];

        for (int i = 0; i < arr.length; i++) {
            double d = arr[i];
            brr[i] = new BigDecimal(arr[i]);
        }
        BigDecimal sum = getArr(brr);
        BigDecimal count = new BigDecimal(arr.length);
        BigDecimal divide = sum.divide(count,2,RoundingMode.HALF_UP);
        System.out.println(divide);
    }

    private static BigDecimal getArr(BigDecimal[] brr) {
        BigDecimal sum = new BigDecimal(0.0);
        for (int i = 0; i < brr.length; i++) {
            sum = sum.add(brr[i]);
        }
        return sum;
    }
}